﻿Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    <ToolboxItem(False)> _
    Friend NotInheritable Class TabControl
        Inherits Control
        ' Methods
        Public Sub New(ByVal dockStyle As DockStyle, ByVal stripAnchor As AnchorAlignment)
            If ((dockStyle = DockStyle.Fill) OrElse (dockStyle = DockStyle.None)) Then
                Throw New ArgumentException(DR.GetString("InvalidDockingStyle", New Object() { "dockStyle" }))
            End If
            MyBase.SuspendLayout
            Me.stripAnchor = stripAnchor
            Me.Dock = dockStyle
            Me.allowDockChange = False
            If ((Me.Dock = DockStyle.Left) OrElse (Me.Dock = DockStyle.Right)) Then
                MyBase.Width = (SystemInformation.VerticalScrollBarWidth + 2)
                Me.splitter = New Splitter
                Me.tabStrip = New TabStrip(Orientation.Vertical, SystemInformation.VerticalScrollBarWidth)
                Me.scrollBar = New VScrollBar
                If (Me.stripAnchor = AnchorAlignment.Near) Then
                    Me.tabStrip.Dock = DockStyle.Top
                    Me.splitter.Dock = DockStyle.Top
                    Me.scrollBar.Dock = DockStyle.Fill
                Else
                    Me.tabStrip.Dock = DockStyle.Bottom
                    Me.splitter.Dock = DockStyle.Bottom
                    Me.scrollBar.Dock = DockStyle.Fill
                End If
            Else
                MyBase.Height = (SystemInformation.HorizontalScrollBarHeight + 2)
                Me.splitter = New Splitter
                Me.tabStrip = New TabStrip(Orientation.Horizontal, SystemInformation.HorizontalScrollBarHeight)
                Me.scrollBar = New HScrollBar
                If (Me.stripAnchor = AnchorAlignment.Near) Then
                    Me.tabStrip.Dock = DockStyle.Left
                    Me.splitter.Dock = DockStyle.Left
                    Me.scrollBar.Dock = DockStyle.Fill
                Else
                    Me.tabStrip.Dock = DockStyle.Right
                    Me.splitter.Dock = DockStyle.Right
                    Me.scrollBar.Dock = DockStyle.Fill
                End If
            End If
            MyBase.Controls.AddRange(New Control() { Me.scrollBar, Me.splitter, Me.tabStrip })
            Me.splitter.Size = New Size(6, 6)
            AddHandler Me.splitter.Paint, New PaintEventHandler(AddressOf Me.OnSplitterPaint)
            AddHandler Me.splitter.DoubleClick, New EventHandler(AddressOf Me.OnSplitterDoubleClick)
            AddHandler DirectCast(Me.TabStrip.Tabs, ItemList([Of] ItemInfo)).ListChanged, New ItemListChangeEventHandler([Of] ItemInfo)(AddressOf Me.OnTabsChanged)
            Me.BackColor = SystemColors.Control
            MyBase.ResumeLayout
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (Not Me.idleHandler Is Nothing) Then
                RemoveHandler Application.Idle, Me.idleHandler
                Me.idleHandler = Nothing
            End If
            MyBase.Dispose(disposing)
        End Sub

        Protected Overrides Sub OnDockChanged(ByVal e As EventArgs)
            If Not Me.allowDockChange Then
                Throw New InvalidOperationException(SR.GetString("Error_ChangingDock"))
            End If
        End Sub

        Private Sub OnIdle(ByVal sender As Object, ByVal e As EventArgs)
            RemoveHandler Application.Idle, Me.idleHandler
            Me.idleHandler = Nothing
            If ((Me.splitter.Dock = DockStyle.Left) OrElse (Me.splitter.Dock = DockStyle.Right)) Then
                If (Not Me.itemsMinimized AndAlso (Me.splitter.SplitPosition <> (MyBase.Width - Me.splitter.MinExtra))) Then
                    Me.splitter.SplitPosition = (MyBase.Width - Me.splitter.MinExtra)
                End If
            ElseIf (Not Me.itemsMinimized AndAlso (Me.splitter.SplitPosition <> (MyBase.Height - Me.splitter.MinExtra))) Then
                Me.splitter.SplitPosition = (MyBase.Height - Me.splitter.MinExtra)
            End If
            If (Me.itemsMinimized AndAlso (Me.splitter.SplitPosition > Me.splitter.MinSize)) Then
                Me.splitter.SplitPosition = Me.splitter.MinSize
            End If
            If (Me.splitter.SplitPosition < Me.splitter.MinSize) Then
                Me.splitter.SplitPosition = Me.splitter.MinSize
            End If
        End Sub

        Protected Overrides Sub OnLayout(ByVal levent As LayoutEventArgs)
            MyBase.OnLayout(levent)
            Dim flag As Boolean = (Me.splitter.SplitPosition < Me.splitter.MinSize)
            If ((Me.splitter.Dock = DockStyle.Left) OrElse (Me.splitter.Dock = DockStyle.Right)) Then
                Dim num As Integer = Math.Max(Me.splitter.MinSize, ((MyBase.Width - Me.tabStrip.MaximumRequiredSize) - Me.splitter.Width))
                If (Me.splitter.MinExtra <> num) Then
                    Me.splitter.MinExtra = num
                End If
                flag = (flag Or If(Me.itemsMinimized, (Me.splitter.SplitPosition <> Me.splitter.MinSize), (Me.splitter.SplitPosition <> (MyBase.Width - Me.splitter.MinExtra))))
            Else
                Dim num2 As Integer = Math.Max(Me.splitter.MinSize, ((MyBase.Height - Me.tabStrip.MaximumRequiredSize) - Me.splitter.Height))
                If (Me.splitter.MinExtra <> num2) Then
                    Me.splitter.MinExtra = num2
                End If
                flag = (flag Or If(Me.itemsMinimized, (Me.splitter.SplitPosition <> Me.splitter.MinSize), (Me.splitter.SplitPosition <> (MyBase.Height - Me.splitter.MinExtra))))
            End If
            If (flag AndAlso (Me.idleHandler Is Nothing)) Then
                Me.idleHandler = New EventHandler(AddressOf Me.OnIdle)
                AddHandler Application.Idle, Me.idleHandler
            End If
        End Sub

        Private Sub OnSplitterDoubleClick(ByVal sender As Object, ByVal e As EventArgs)
            Me.itemsMinimized = Not Me.itemsMinimized
            If Not Me.itemsMinimized Then
                Me.splitter.SplitPosition = (If(((Me.splitter.Dock = DockStyle.Left) OrElse (Me.splitter.Dock = DockStyle.Right)), MyBase.Width, MyBase.Height) - Me.splitter.MinExtra)
            Else
                Me.splitter.SplitPosition = Me.splitter.MinSize
            End If
        End Sub

        Private Sub OnSplitterPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
            Dim clientRectangle As Rectangle = MyBase.ClientRectangle
            If ((Me.splitter.Dock = DockStyle.Left) OrElse (Me.splitter.Dock = DockStyle.Right)) Then
                e.Graphics.DrawLine(SystemPens.ControlLightLight, 0, 0, 0, Me.splitter.Height)
                e.Graphics.DrawLine(SystemPens.ControlLightLight, 0, 0, 5, 0)
                e.Graphics.DrawLine(SystemPens.ControlDark, 4, 0, 4, (Me.splitter.Height - 1))
                e.Graphics.DrawLine(SystemPens.ControlDark, 4, (Me.splitter.Height - 1), 0, (Me.splitter.Height - 1))
                e.Graphics.DrawLine(SystemPens.ControlText, 5, 0, 5, Me.splitter.Height)
            Else
                e.Graphics.DrawLine(SystemPens.ControlLightLight, 0, 1, Me.splitter.Width, 1)
                e.Graphics.DrawLine(SystemPens.ControlLightLight, 0, 1, 0, 5)
                e.Graphics.DrawLine(SystemPens.ControlDark, 0, 4, Me.splitter.Width, 4)
                e.Graphics.DrawLine(SystemPens.ControlDark, (Me.splitter.Width - 1), 4, (Me.splitter.Width - 1), 1)
                e.Graphics.DrawLine(SystemPens.ControlText, 0, 5, Me.splitter.Width, 5)
            End If
        End Sub

        Private Sub OnTabsChanged(ByVal sender As Object, ByVal e As ItemListChangeEventArgs([Of] ItemInfo))
            If ((Me.splitter.Dock = DockStyle.Left) OrElse (Me.splitter.Dock = DockStyle.Right)) Then
                Me.splitter.MinExtra = ((MyBase.Width - Me.tabStrip.MaximumRequiredSize) - Me.splitter.Width)
                Me.splitter.MinSize = Me.tabStrip.MinimumRequiredSize
            ElseIf ((Me.splitter.Dock = DockStyle.Top) OrElse (Me.splitter.Dock = DockStyle.Bottom)) Then
                Me.splitter.MinExtra = ((MyBase.Height - Me.tabStrip.MaximumRequiredSize) - Me.splitter.Height)
                Me.splitter.MinSize = Me.tabStrip.MinimumRequiredSize
            End If
        End Sub


        ' Properties
        Public ReadOnly Property ScrollBar As ScrollBar
            Get
                Return Me.scrollBar
            End Get
        End Property

        Public ReadOnly Property TabStrip As TabStrip
            Get
                Return Me.tabStrip
            End Get
        End Property


        ' Fields
        Private allowDockChange As Boolean = True
        Private idleHandler As EventHandler
        Private itemsMinimized As Boolean = True
        Private scrollBar As ScrollBar
        Private splitter As Splitter
        Private Const SplitterSize As Integer = 6
        Private stripAnchor As AnchorAlignment
        Private tabStrip As TabStrip
    End Class
End Namespace

